﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace Little.Soldier.SWIFT.Validation
{
  [Serializable]
  public abstract class ValidatePropertyBase : IValidate
  {
    private bool _Result = true;
    private List<ValidationRuleBase> _Rules = new List<ValidationRuleBase>();
    private PropertyInfo _ValidateProperty;
    private object _ParentPropertyObject;
    private bool _AllowEmptyValue;

    public bool AllowEmptyValue
    {
      get
      {
        return this._AllowEmptyValue;
      }
      set
      {
        this._AllowEmptyValue = value;
      }
    }

    public virtual bool Result
    {
      get
      {
        return this._Result;
      }
    }

    public virtual object ParentPropertyObject
    {
      get
      {
        return this._ParentPropertyObject;
      }
      set
      {
        this._ParentPropertyObject = value;
      }
    }

    public virtual string PropertyName
    {
      get
      {
        return this._ValidateProperty.Name;
      }
    }

    public virtual List<ValidationRuleBase> Rules
    {
      get
      {
        return this._Rules;
      }
    }

    public PropertyInfo ValidateProperty
    {
      get
      {
        return this._ValidateProperty;
      }
      set
      {
        this._ValidateProperty = value;
      }
    }

    protected ValidatePropertyBase(PropertyInfo validateProperty, object parentPropertyObject)
    {
      this._ValidateProperty = validateProperty;
      this._ParentPropertyObject = parentPropertyObject;
      this.InitializeRules();
    }

    protected ValidatePropertyBase(PropertyInfo validateProperty, object parentPropertyObject, bool allowEmptyValue)
    {
      this._ValidateProperty = validateProperty;
      this._ParentPropertyObject = parentPropertyObject;
      this._AllowEmptyValue = allowEmptyValue;
      this.InitializeRules();
    }

    protected abstract void InitializeRules();

    internal void U0002U0002(bool result)
    {
      if (!this.Result)
        return;
      this._Result = result;
    }

    private void U0086U0002(ValidationError[] newErrors, List<ValidationError> errorList)
    {
      if (newErrors == null || newErrors.GetLength(0) == 0)
      {
        this.U0002U0002(true);
      }
      else
      {
        foreach (ValidationError validationError in newErrors)
        {
          validationError.ParentPropertyObject = this.ParentPropertyObject;
          validationError.PropertyName = this.PropertyName;
          errorList.Add(validationError);
        }
        this.U0002U0002(false);
      }
    }

    public virtual bool Validate(List<ValidationError> errorList)
    {
      if (errorList == null)
        goto label_2;
label_1:
      string str = string.Empty;
      object obj = this._ValidateProperty.GetValue(this._ParentPropertyObject, (object[]) null);
      this.Validate(obj != null ? obj.ToString() : string.Empty, errorList);
      return this._Result;
label_2:
      errorList = new List<ValidationError>();
      goto label_1;
    }

    private bool Validate(string value, List<ValidationError> errorList)
    {
      if (string.IsNullOrEmpty(value) && this.AllowEmptyValue)
        return this._Result;
      foreach (ValidationRuleBase validationRuleBase in this.Rules)
      {
        string str = value;
        validationRuleBase.U0003U0003(ref str);
        this.U0086U0002(validationRuleBase.Validate(str), errorList);
      }
      return this._Result;
    }
  }
}
